/*
 * @Descripttion: 出手鸭小程序
 * @version:1.0.1 
 * @Author: HHL
 * @Date: 2023-10-11 18:29:20
 * @LastEditTime: 2024-08-11 21:18:14
 * @LastEditors: sueRimn
 */

// 估价信息
import {
  fetchUserEvaluateResult,
  fetchHotList,
} from '@/api'

const state = {
  evaluateResult: uni.getStorageSync('evaluateResult') || {}, // 估价信息

  userEvaluateInfo: {}, // 用户最近估价信息

  hotPhone: {}, // 热门回收机型
  countdown:{
  	  hours:'24',
  	  minutes:'00',
  	  seconds:'00'
  },//倒计时
  recoveryMode:0//回收方式
}

const mutations = {
  // 更新估价信息
  UPDATE_EVALUATE: (state, data) => {
    state.evaluateResult = data
  },

  // 更新用户最近估价信息
  UPDATE_USER_LAST_EVALUATE: (state, data) => {
    state.userEvaluateInfo = data
  },

  // 热门回收机型
  UPDATE_HOT_PHONE: (state, data) => {
    state.hotPhone = data
  },
  // 更新倒计时
  UPDATE_COUNTDOWN: (state, countdown) => {
    state.countdown = countdown
  },
  // 更新回收方式
  UPDATE_MODE: (state, mode) => {
    state.recoveryMode = mode
  }
}

const actions = {
  // 估价信息
  updateEvaluateInfo({ commit }, evaluate) {
    uni.setStorageSync('evaluateResult', evaluate)
    commit('UPDATE_EVALUATE', evaluate)
    commit('UPDATE_USER_LAST_EVALUATE', {
      ...evaluate, fromType: 'last', fromText: '最近'
    })
  },


  // 用户最近估价信息
  loaduserEvaluateInfo({ commit }) {
    return new Promise(resolve => {
      fetchUserEvaluateResult().then(({ data, code }) => {
        if (code === 200 && data && data.productId) {
          const info = { ...data, fromType: 'last', fromText: '最近' }
          commit('UPDATE_USER_LAST_EVALUATE', info)
          resolve(info)
        } else {
          resolve({})
        }
      })
    })
  },

  // 热门回收机型
  loadHotPhone({ commit }) {
    return new Promise(resolve => {
      fetchHotList({ size: 1, page: 1 }).then(({ data, code }) => {
        if (code === 200 && data && data.length) {
          let product = data[0]
          product.evaluatePrice = product.maxPrice || 0
          const info = { ...product, fromType: 'hot', fromText: '热门' }
          commit('UPDATE_HOT_PHONE', info)
          resolve(info)
        } else {
          resolve({})
        }
      })
    })
  },
  // 更新倒计时
  updateCountdown({ commit }, countdown) {
    commit('UPDATE_COUNTDOWN', countdown)
  },
  // 更新用户回收方式
  updateMode({ commit }, mode) {
    commit('UPDATE_MODE', mode)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}